package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private  GraphInfoMapper graphInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    public AgreementDetailVo queryLeaseDetailById(Long id) {
        //根据租约id获取租约信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        if (ObjectUtils.isEmpty(leaseAgreement)) {
            return  null;
        }
        //通过leaseAgreement中的房间id查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());
        //通过roomInfo的主键id查询房间图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,roomInfo.getId());
        List<GraphInfo> graphInfoList = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper);
        //将graphInfoList转换为将graphVoList转换为GraphVo
        List<GraphVo> graphVos;
        if (!CollectionUtils.isEmpty(graphInfoList)) {
            graphVos = new ArrayList<>(graphInfoList.size());
            GraphVo graphVo = new GraphVo();
            for (GraphInfo graphInfo : graphInfoList) {
                graphVo.setName(graphInfo.getName());
                graphVo.setUrl(graphInfo.getUrl());
                graphVos.add(graphVo);
            }
        }else {
            graphVos=null;
        }
        //通过roomInfo中的公寓id查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //通过apartmentInfo中的id查询公寓图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper1.eq(GraphInfo::getItemId,apartmentInfo.getId());
        graphInfoLambdaQueryWrapper1.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        List<GraphInfo> apartmentgraphInfoList = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper1);
        //将graphInfoList转换为将graphVoList转换为GraphVo
        List<GraphVo> apartmentgraphVos;
        if (!CollectionUtils.isEmpty(apartmentgraphInfoList)) {
            apartmentgraphVos = new ArrayList<>(apartmentgraphInfoList.size());
            GraphVo graphVo = new GraphVo();
            for (GraphInfo graphInfo : apartmentgraphInfoList) {
                graphVo.setName(graphInfo.getName());
                graphVo.setUrl(graphInfo.getUrl());
                apartmentgraphVos.add(graphVo);
            }
        }else {
            apartmentgraphVos= null;
        }
        //通过leaseAgreement中的支付方式id查询支付方式
        PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());
        //通过leaseAgreement中的租期id查询租期属性
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());

        //汇总vo
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement,agreementDetailVo);
        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        agreementDetailVo.setApartmentGraphVoList(apartmentgraphVos);
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        agreementDetailVo.setRoomGraphVoList(graphVos);
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());

        return agreementDetailVo;
    }
}




